#!/usr/bin/env python3
# coding:utf-8

import asyncio
import aiomqtt

async def main():
    try:
        async with aiomqtt.Client('127.0.0.1', timeout=10) as client:
            await client.subscribe('test/topic')
            async for msg in client.messages:
                print("recv: ", msg.topic, msg.payload)
    except aiomqtt.MqttError as e:
        print(f"MQTT error: {e}")
    except asyncio.CancelledError:
        print("Operation was cancelled")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == '__main__':
    asyncio.run(main())
